{
 "nbformat": 4,
 "nbformat_minor": 2,
 "metadata": {
  "language_info": {
   "name": "python",
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "version": "3.7.4-final"
  },
  "orig_nbformat": 2,
  "file_extension": ".py",
  "mimetype": "text/x-python",
  "name": "python",
  "npconvert_exporter": "python",
  "pygments_lexer": "ipython3",
  "version": 3,
  "kernelspec": {
   "name": "python37432bit8d8e1828c3004b5281cefc61c8c7a538",
   "display_name": "Python 3.7.4 32-bit"
  }
 },
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "s1 = pd.Series([4,7,-5,3])#创建一个series，索引为默认值\n",
    "print(s1.values) #series的值\n",
    "print(s1.index) #series的索引\n",
    "s2 = pd.Series([4.0,6.5,-0.5,4.2],index=['d','b','a','c'])\n",
    "print(s2['a']) #根据索引取值\n",
    "print(s2[['a','b','c']]) #根据索引取值\n",
    "print('b' in s2) # in 判断\n",
    "dic1 = {'apple':5,'pen':3,'applepen':10}\n",
    "s3 = pd.Series(dic1) #Series可以看成是一个定长的有序字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = {'year':[2014,2015,2016,2017],\n",
    "        'income':[10000,30000,50000,80000],\n",
    "        'pay':[5000,20000,30000,30000]\n",
    "}\n",
    "df1 = pd.DataFrame(data)\n",
    "df2 = pd.DataFrame(np.arange(12).reshape((3,4)))\n",
    "df3 = pd.DataFrame(np.arange(12).reshape((3,4)),index=['a','c','b'],columns=[2,33,44,5])\n",
    "print(df1.columns )#列 df1.index #行  df1.values # 值\n",
    "df1.describe()\n",
    "print(df1.T)\n",
    "df3.sort_index(axis=1)#列排序 axis=0 #行排序\n",
    "df3.sort_values(by=44)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dates = pd.date_range('20170101',periods=6)\n",
    "df1 = pd.DataFrame(np.arange(24).reshape((6,4)),index=dates,columns=['A','B','C','D'])\n",
    "df1['A']#将DataFrame的列获取为一个Series 或者df1.A\n",
    "df1[0:2]#取0-1行\n",
    "df1['20170102':'20170104']\n",
    "df1.loc['20170102'] #通过标签选择数据\n",
    "df1.loc['20170101',['A','C']]\n",
    "df1.loc[:,['A','B']]\n",
    "df1.iloc[2] #第二行 #通过位置选择数据\n",
    "df1.iloc[1:3,2:4]\n",
    "df1.iloc[[1,2,4],[1,3]]\n",
    "df1.ix[2:4,['A','C']] #混合标签位置选择\n",
    "df1[df1.A>6] #关系选择"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df1.A[df1.A==0] = 1 # 选择数据后进行赋值\n",
    "df1['E'] = 10 # 不存在则 添加一列\n",
    "df1['F'] = pd.Series([1,2,3,4,5,6],index=dates)#添加一列\n",
    "# df2 = df1.append(pd.Series())\n",
    "# df1.insert(1,'G',df2['E'])#在第一列插入索引为G的df2中的E列\n",
    "g = df1.pop('G')#弹出G列 或者 del df1['G']#删除G列\n",
    "df2 = df1.drop(['A','B'],axis=1)#删除AB列 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df2.dropna(axis=0,how='any') #axis=[0,1] 0代表行，1代表列。\n",
    "# how=['any','all'] any任意一个或多个 all全部 删除丢失数据\n",
    "df2.fillna(value=0)#把空值赋值为0\n",
    "df2.isnull()#查看空值 在位置标记True ，False\n",
    "np.any(df2.isnull())#只要有一个或多个空值就会返回true "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# file = pd.read_csv('people.csv',encoding='gbk') # 读取文件\n",
    "# file.to_csv('people2.csv') # 保存"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df3 = pd.concat([df1,df2],axis=0)#纵向合并\n",
    "df4 = pd.concat([df1,df2,df3],axis=0,ignore_index=True)#纵向合并，不考虑原来的index\n",
    "df6 = pd.concat([df1,df2],join='outer',ignore_index=True)#合并两个表，缺少的部分填充NaN \n",
    "df7 = pd.concat([df1,df2],join='inner',ignore_index=True)#合并两个表，缺少的部分去掉\n",
    "df8 = pd.concat([df1,df2],axis=1,join_axes=[df1.index])#横向合并，index使用df1的index\n",
    "#how = ['left','right','inner','outer']\n",
    "res = pd.merge(left,right,on=['key1','key2'],how='outer')#how默认inner\n",
    "res = pd.merge(left,right,left_index=True,right_index=True,how='outer')\n",
    "res = pd.merge(boys,girls,on='k',suffixes=['_boy','_girl'],how='outer')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = pd.Series(np.random.randn(1000),index=np.arange(1000))\n",
    "data = data.cumsum()\n",
    "data.plot()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = pd.DataFrame(np.random.randn(1000,4),index=np.arange(1000),columns=['A','B','C','D'])\n",
    "data = data.cumsum()\n",
    "p# rint(data.head())\n",
    "data.plot()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ]
}